<template>
	<div>
		<van-nav-bar
		  title="收货地址"
			left-text="返回"
			 @click-left="handleLeftClick"
		/>
		<p-page-helper :disable="loading" @on-end="handleOnEnd">
			<van-address-list
			  v-model="chosenAddressId"
			  :list="list"
				:switchable="false"
			  default-tag-text="默认"
			  @add="onAdd"
			  @edit="onEdit"
			/>
		</p-page-helper>
	</div>
</template>

<script>
	import {getAddressListForPage} from '@/api/address-api.js'
	export default{
		data(){
			return {
				list:[],
				queryForm:{
					pno:1,
					psize:10
				},
				page:{},
				loading:false,
				chosenAddressId:1
			}
		},
		async created() {
			await this.getAddressListForPage()
		},
		methods:{
			handleLeftClick(){
				this.$router.history.go(-1)
			},
			async getAddressListForPage(){
				let res = await getAddressListForPage(this.queryForm)
				if(res.data.code == 200){
					this.list = res.data.data.list.map(item => {
						return {
							...item,
							tel:item.phone,
							address:item.province+item.city+item.area+item.address
						}
					})
					this.page = res.data.data.page
				}
			},
			async handleOnEnd(){
				if(this.loading ==false){
					this.loading = true
				}
				if(this.page.pno<this.page.pCount){
					this.queryForm.pno = this.queryForm.pno+1
					let res = await getAddressListForPage(this.queryForm)
					if(res.data.code == 200){
						this.list = this.list.concat(res.data.data.list.map(item => {
							return {
								...item,
								tel:item.phone,
								address:item.province+item.city+item.area+item.address
							}
						}))
						this.page = res.data.data.page
						this.loading = false
					}
				}
			},
			onAdd()	{
				this.$router.push('/user-address-add')
			},
			onEdit(item){
				this.$router.push({path:'/user-address-edit',query:{id:item.id}})
			}
		}
	}
</script>

<style>
</style>
